/*
 * @version: 1.0
 * @Author: zhl
 * @Date: 2024-12-20 11:20:27
 * @LastEditors: zhl
 * @Description: file content
 */
/*
 *                        _oo0oo_
 *                       o8888888o
 *                       88" . "88
 *                       (| -_- |)
 *                       0\  =  /0
 *                     ___/`---'\___
 *                   .' \\|     |// '.
 *                  / \\|||  :  |||// \
 *                 / _||||| -:- |||||- \
 *                |   | \\\  - /// |   |
 *                | \_|  ''\---/''  |_/ |
 *                \  .-\__  '-'  ___/-. /
 *              ___'. .'  /--.--\  `. .'___
 *           ."" '<  `.___\_<|>_/___.' >' "".
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /
 *      =====`-.____`.___ \_____/___.-`___.-'=====
 *                        `=---='
 * 
 * 
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * 
 *            佛祖保佑       永不宕机     永无BUG
 * 
 *        佛曰:  
 *                写字楼里写字间，写字间里程序员；  
 *                程序人员写程序，又拿程序换酒钱。  
 *                酒醒只在网上坐，酒醉还来网下眠；  
 *                酒醉酒醒日复日，网上网下年复年。  
 *                但愿老死电脑间，不愿鞠躬老板前；  
 *                奔驰宝马贵者趣，公交自行程序员。  
 *                别人笑我太疯癫，我笑自己命忒贱；  
 *                不见满街漂亮妹，哪个归得程序员？
 * 
 */

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>

#include "version.h"
#include "logger.h"
#include "dbManager.h"
#include "consoleColor.h"
#include "mqttManager.h"
#include "mqttLocal.h"
#include "Settings.h"
#include "settingsManager.h"





#define ROOT_DIR  ".."
char  g_conf_path[256] = ROOT_DIR"/conf/ped.conf";
char  g_db_path[256] = ROOT_DIR"/db/ped.db";
char  g_module_path[256] = ROOT_DIR"/model/";




void usage(){
    printf(GREEN"Usage: ./ped [options]\n"NONE);
    printf(GREEN"Options:\n"NONE);
    printf(GREEN"  -l, --level <level>   Set the level of the logger\n"NONE);
    printf(GREEN"  -v, --version         Show the version of the program\n"NONE);
    printf(GREEN"  -c, --config <path>   Set the path of the configuration file\n"NONE);
    printf(GREEN"  -d, --dpPath <path> Set the path of the database file\n"NONE);
    printf(GREEN"  -m, --module <path>   Set the path of the module file\n"NONE);
    printf(GREEN"      --ip,             set mqtt broker address\n"NONE);
    printf(GREEN"  -p, --port            set mqtt broker port\n"NONE);
    printf(GREEN"  -u, --user            set mqtt broker user name\n"NONE);
    printf(GREEN"      --password        set mqtt broker password\n"NONE);
    printf(GREEN"  -v, --version         Show the version of the program\n"NONE);
    printf(GREEN"  -h, --help            Show this help message\n"NONE);


}

static struct option long_options[] = {
                                        {"level",    required_argument, 0,  'l'},
                                        {"version",  no_argument,       0,  'v'},
                                        {"config",   required_argument, 0,  'c'},
                                        {"dbPath",   required_argument, 0,   1 },
                                        {"module",   required_argument, 0,   2 },
                                        {"ip",       required_argument, 0,   3 },
                                        {"user",     required_argument, 0,  'u'},
                                        {"password", required_argument, 0,   4},
                                        {"port",     required_argument, 0,  'p'},
                                        {"help",     required_argument, 0,  'h'},
                                        {0,          0,                 0,   0 }
                                    };


int parse_args(int argc, char *argv[]) {
    int c;
    int digit_optind = 0;
 
    int option_index = 0;

   while (-1 == (c = getopt_long(argc, argv, "l:h?", long_options, &option_index))) {
        int this_option_optind = optind ? optind : 1;
 
       switch (c) {
        case 0:
            printf("option %s", long_options[option_index].name);
            if (optarg)
                printf(" with arg %s", optarg);
            printf("\n");
            break;
 
       case 1:
            snprintf(g_db_path, sizeof(g_db_path), "%s", optarg);
            break;
       case 2:
            snprintf(g_module_path, sizeof(g_module_path), "%s", optarg);
            break;
       case 3:
            snprintf(g_local_mqtt_addr, sizeof(g_local_mqtt_addr), "%s", optarg);
            break;
       case 'p':
            g_local_mqtt_port = atoi(optarg);
            break;
       case 'u':
            snprintf(g_local_mqtt_username, sizeof(g_local_mqtt_username), "%s", optarg);
            break;
       case 4:
            snprintf(g_local_mqtt_password, sizeof(g_local_mqtt_password), "%s", optarg);
            break;
       case 'l':
            g_log_level = atoi(optarg);
            if(0 > g_log_level || 7 < g_log_level) {
                printf(RED"Invalid log level, default(ELOG_LVL_INFO)\n"NONE);
                g_log_level = ELOG_LVL_INFO;
            }
            break;
 
       case 'b':
            printf("option b\n");
            break;
 
       case 'c':
            snprintf(g_conf_path, sizeof(g_conf_path), "%s", optarg);
            break;
 
       case 'd':
            printf("option d with value '%s'\n", optarg);
            break;
 
       case '?':
            usage();
            return -1;
            break;
 
       default:
            printf("?? getopt returned character code 0%o ??\n", c);
            usage();
            break;
        }
    }
    return 0;
}



int main(int argc, char *argv[]) {
    //初始化参数
    if(0 > parse_args(argc, argv)) {
        return -1;
    }
    //初始化日志
    logger_init(APPNAME, g_log_level);
    add_file_logger(APPNAME, 10*1024*1024, 1);
    //初始化配置
    loadSettings();
    //初始化本地mqtt
    localMqttInit();
    //初始化数据库
    dbManagerInit();
    //初始化mqtt
    //
    //初始化配置,从数据库中读取配置

    //初始化通信模块


    return 0;
}